<?php

require_once ROOT_PATH . "/config/Database.php";

class UserDao
{
        private $db;
        // public function __construct(Database $db)
        // {
        //         $this->db = $db;
        // }
        // public function __construct()
        // {
        //         $this->db = new Database();
        // }
        public function setDb($db)
        {
                $this->db = $db;
        }

        public function addUser($name, $password, $email, $role)
        {
                error_log("db==>" . $this->db);
                $this->db->execute("INSERT INTO `users` (`username`, `password`,`email`, `role`) VALUES (?,?,?,?)", [$name, $password, $email, $role]);
        }
        public function getUsers($limit)
        {
                return $this->db->select("SELECT * FROM users ORDER BY id ASC LIMIT ?", [$limit]);
        }
        public function getUsersAll()
        {
                return $this->db->select("select * from users ");

        }
        public function getUserById($id)
        {
                return $this->db->select("SELECT * FROM users WHERE id = ?", [$id])[0];
        }

        public function updateUser($user)
        {
                $this->db->execute("UPDATE users SET   password =?, email =?,avatar =? , bio=?,  role =? WHERE id = ?", [$user->password, $user->email, $user->avatar, $user->bio, $user->role, $user->id]);
        }

        public function deleteUser($id)
        {
                $this->db->execute("DELETE FROM users WHERE id = ?", [$id]);
        }
}